<template>
  <div class="container">
    <p style=" text-align: justify; text-indent: 0.5em;">欢迎用户：{{currentuser}}</p>

    <el-row>
      <el-col :span="3">
        <div class="grid-content">
          <el-button type="primary" icon="el-icon-s-home" round align="right" @click="main()">首页</el-button>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="grid-content">
          <el-button
            v-if="this.currentlevel>2"
            type="primary"
            icon="el-icon-right"
            round
            align="right"
            @click="back()"
          >上级目录</el-button>
        </div>
      </el-col>
      <el-col :span="5" offset="8">
        <div class="grid-content">
          <el-button
            type="warning"
            icon="el-icon-upload2"
            round
            align="right"
            @click="dialogVisible = true"
          >点击上传文件</el-button>
        </div>

        <el-dialog title="上传窗口" :visible.sync="dialogVisible" width="60%">
          <el-upload
            class="upload-demo"
            action
            :limit="1"
            :file-list="formFileList"
            :http-request="handleUploadForm"
            :on-exceed="formHandleExceed"
            :on-remove="formHandleRemove"
            :before-upload="beforeUploadForm"
          >
            <el-button type="primary">上传文件</el-button>
            <span
              slot="tip"
              class="el-upload__tip"
              style="margin: 0 10px;"
            >文件不超过{{formMaxSize}}M</span>
          </el-upload>
        </el-dialog>
      </el-col>
      <el-col :span="1">
        <div class="grid-content">
          <el-button type="warning" round align="right" @click="open">+ 新建文件夹</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24"></el-col>
      <el-table :data="tableData2" style="width: 100%">
        <el-table-column label="文件夹列表" width="250px">
          <template slot-scope="scope">
            <el-button round>{{ scope.row.id}}</el-button>
          </template>
        </el-table-column>

        <el-table-column width="350px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="sondir(scope.row)"
              icon="el-icon-folder-opened"
              round
              width="100px"
              class="sub-btn sub2"
            >{{ scope.row.name}}</el-button>
          </template>
        </el-table-column>

        <el-table-column width="200px">
          <template slot-scope="scope">
            <el-button type="danger" icon="el-icon-delete" circle @click="deletee(scope.row)"></el-button>
            <el-button type="info" icon="el-icon-download" circle></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="文件列表" width="250px">
          <template slot-scope="scope">
            <el-button round>{{scope.row.id}}</el-button>
          </template>
        </el-table-column>
        <el-table-column width="350px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-notebook-2"
              round
              width="100px"
              class="sub-btn sub2"
            >{{ scope.row.name}}</el-button>
          </template>
        </el-table-column>
        <el-table-column width="200px">
          <template slot-scope="scope">
            <el-button type="danger" icon="el-icon-delete" @click="deletee(scope.row)" circle></el-button>
            <el-button type="success" icon="el-icon-download" circle @click="download(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </div>
</template>


<script>
export default {
  data() {
    return {
      formMaxSize: 20, // 上传文件大小
      formFileList: [], // 显示上传文件
      uploadFormFileList: [], // 确定上传文件
      filelists: [],
      dialogVisible: false,
      tableData: [],
      tableData2: [],
      currentpath: "",
      currentuser: "ufo",
      currentlevel: "",
      input: ""
    };
  },
  methods: {
    // 开始上传前验证
    beforeUploadForm(file) {
      // 验证文件大小
      if (file.size / 2048 / 2048 > this.formMaxSize) {
        this.$message({
          message: `上传文件大小不能超过${this.formMaxSize}M!`,
          type: "warning"
        });
        return false;
      }
      // 中文乱码处理
      if (file.raw) {
        let reader = new FileReader(); // 读取文件内容
        reader.readAsText(file.raw, "gb2312"); // 防止中文乱码问题，不加reader.onload方法都不会触发
        reader.onload = function(e) {
          this.contentHtml = e.target.result; // txt文本内容，接下来就可以对其进行校验处理了
        };
      }
      // 验证文件类型
      // var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      // const extension =testmsg === "mp3" || testmsg === "txt" || testmsg === "csv";
      // if (!extension) {
      //   this.$message({
      //     message: "mp3/txt/csv格式!",
      //     type: "warning"
      //   });
      // }
      // return extension;
    },
    // 移除上传列表中文件
    formHandleRemove(file, formFileList) {
      let thiz = this;
      for (let i = 0; i < thiz.uploadFormFileList.length; i++) {
        if (thiz.uploadFormFileList[i].pname === file.name) {
          thiz.uploadFormFileList.splice(i, 1);
          break;
        }
      }
    },
    // 允许上传文件个数验证
    formHandleExceed(files, formFileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + formFileList.length} 个文件`
      );
    },
    // 上传文件
    handleUploadForm(param) {
      let thiz = this;
      let formData = new FormData();
      formData.append("path", "hdfs://localhost:9000/" + this.currentpath); // 额外参数
      formData.append("file", param.file);
      let loading = thiz.$loading({
        lock: true,
        text: "上传中，请稍候...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      thiz.$http
        .post("http://123.57.56.140:11111/hdfs/do/list/upload2", formData)
        .then(({ data }) => {
          if (data.code != 0) {
            thiz.$message.success("上传文件成功");
            this.freshcurrent();
            this.dialogVisible = false;
            thiz.formFileList = [];
            thiz.uploadFormFileList = [];
          } else {
            thiz.formFileList = [];
            thiz.uploadFormFileList = [];
            thiz.$message("上传文件失败，" + data.message);
          }
          loading.close();
        });
    },
    getObjectURL(file) {
      var url = null;
      if (window.createObjcectURL != undefined) {
        url = window.createOjcectURL(file);
      } else if (window.URL != undefined) {
        url = window.URL.createObjectURL(file);
      } else if (window.webkitURL != undefined) {
        url = window.webkitURL.createObjectURL(file);
      }
      return url;
    },

    openFile: function() {
      document.getElementById("open").click();
    },
    showRealPath: function() {
      var filelists = document.getElementById("open").files;
      for (var i = 0; i < filelists.length; i++) {
        console.log(this.getObjectURL(filelists[i]));
        console.log(filelists[i].path);
      }
    },

    open() {
      this.$prompt("重名将不会被创建哦~", "新建文件夹", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(({ value }) => {
          this.$message({
            type: "success",
            message: "新建文件夹: " + value
          });
          this.newdir(value);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入"
          });
        });
    },
    newdir(dirname) {
      this.$http
        .post("http://123.57.56.140:11111/hdfs/do/list/mkdir", {
          url: "/" + this.currentpath,
          newdir: "/" + dirname
        })
        .then(({ data }) => {
          this.freshcurrent();
        })
        .catch(error => {
          console.error("网络错误");
        });
    },
    freshcurrent() {
      if (this.currentlevel == 1) {
        this.main();
      } else {
        this.$http
          .post("http://123.57.56.140:11111/hdfs/do/list/son", {
            url: "hdfs://localhost:9000/" + this.currentpath
          })
          .then(({ data }) => {
            console.log(data);
            console.log(this.currentlevel);
            this.tableData = data.filedata;
            this.tableData2 = data.dirdata;
          })
          .catch(error => {
            console.error("网络错误");
          });
      }
    },
    deletee(data) {
      console.log(213123);
      console.log(data);
      this.$http
        .post(`http://123.57.56.140:11111/hdfs/do/list/delete`, {
          url: data.url
        })
        .then(({ data }) => {
          this.freshcurrent();
        })
        .catch(error => {
          console.error("网络错误");
        });
    },
    main() {
      this.$http
        .post(`http://123.57.56.140:11111/hdfs/do/list/${this.currentuser}`)
        .then(({ data }) => {
          console.log(data);
          this.currentlevel = 1;
          this.tableData = data.filedata;
          this.tableData2 = data.dirdata;
          this.currentpath = data.currentpath;
        })
        .catch(error => {
          console.error("网络错误");
        });
    },
    getbackpath(a) {
      console.log("dnscsancn");
      console.log(a);
      var sum = a.length - 1;
      for (var i = 0; i < sum; i++) {
        if (a.charAt(a.length - 1) == "/") {
          break;
        } else {
          a = a.substr(0, a.length - 1);
        }
      }
      this.currentpath = a;
      this.$http
        .post("http://123.57.56.140:11111/hdfs/do/list/son", {
          url: a
        })
        .then(({ data }) => {
          console.log(data);
          console.log(this.currentlevel);
          this.tableData = data.filedata;
          this.tableData2 = data.dirdata;
        })
        .catch(error => {
          console.error("网络错误");
        });
    },
    back() {
      if (this.currentlevel > 2) {
        console.log("caqwd");
        var a = this.currentpath;
        var a = this.getbackpath(a);
        this.currentlevel = this.currentlevel - 1;
      }
    },
    sondir(data) {
      console.log(111);
      console.log(data.url); //他打印了整个data
      console.log(this.$http);
      this.currentpath = data.url;
      this.$http
        .post("http://123.57.56.140:11111/hdfs/do/list/son", {
          url: data.url
        })
        .then(({ data }) => {
          console.log(data);
          this.currentlevel = this.currentlevel + 1;
          console.log(this.currentlevel);
          this.tableData = data.filedata;
          this.tableData2 = data.dirdata;
          this.currentpath = data.currentpath;
        })
        .catch(error => {
          console.error("网络错误");
        });
    },
    download(data) {
      console.log(data.downloadurl);
      window.open(data.downloadurl);
    },
    getdata(url) {}
  },
  created() {
    this.currentuser = this.$route.params.username;
    console.log(this.currentuser);
    this.$http
      .post(`http://123.57.56.140:11111/hdfs/do/list/${this.currentuser}`)
      .then(({ data }) => {
        this.currentpath = data.currentpath;
        console.log(data);
        this.currentlevel = 1;
        this.tableData = data.filedata;
        this.tableData2 = data.dirdata;
      })
      .catch(error => {
        console.error("网络错误");
      });
  }
};
</script>

<style>
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.container {
  margin: 0 auto;
  width: 800px;
}
.cc {
  margin: 0 auto;
}
.sub2 {
  color: #4194f6;
  background: none;
  border-color: #4194f6;
  width: 270px;
}
.sub2:active {
  background: #3a8ee6;
  border-color: #3a8ee6;
  color: #fff;
}
p {
}
</style>
